package com.ljx.blog.article.controller;

import com.ljx.blog.article.domain.entity.Category;
import com.ljx.blog.article.domain.vo.req.ArticlePageReq;
import com.ljx.blog.article.domain.vo.req.CategorySubmitReq;
import com.ljx.blog.article.domain.vo.resp.ArticleResp;
import com.ljx.blog.article.domain.vo.resp.CategoryResp;
import com.ljx.blog.article.service.ArticleService;
import com.ljx.blog.article.service.CategoryService;
import com.ljx.blog.common.domain.vo.resp.ApiResult;
import com.ljx.blog.common.domain.vo.resp.PageResp;
import com.ljx.blog.security.annotation.ApiPermission;
import com.ljx.blog.security.common.PermissionConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-10-12  22:04
 */
@Api(tags = "分类接口")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ArticleService articleService;

    @ApiOperation("获取分类信息")
    @GetMapping("/info/{id}")
    public ApiResult<CategoryResp> getInfo(@PathVariable("id") Long CategoryId){
        return ApiResult.success(categoryService.getInfo(CategoryId));
    }

    @ApiPermission(permissions = {PermissionConstant.CATEGORY_NEW_EDIT})
    @ApiOperation("保存/更新分类")
    @PostMapping("/submit")
    public ApiResult<CategoryResp> submit(@Validated @RequestBody CategorySubmitReq req){
        return ApiResult.success(categoryService.submit(req));
    }

    @ApiOperation("获取分类下的文章")
    @GetMapping("/article/page")
    public ApiResult<PageResp<ArticleResp>> getArticlePage(ArticlePageReq req){
        return ApiResult.success(articleService.getPage(req));
    }

    @ApiOperation("获取租户分类列表")
    @GetMapping("/list")
    public ApiResult<List<CategoryResp>> getList(){
        return ApiResult.success(categoryService.getList());
    }

    @ApiPermission(permissions = {PermissionConstant.CATEGORY_LIST, PermissionConstant.ADMIN_LIST})
    @ApiOperation("获取租户分类列表（管理员）")
    @GetMapping("/admin/list")
    public ApiResult<List<Category>> getListByAdmin(){
        return ApiResult.success(categoryService.getListByAdmin());
    }

}
